package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cjgl.entity.Teacher;
import service.TeacherService;

/**
 * Servlet implementation class ZcServlet
 */
@WebServlet("/zc")
public class ZcServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private TeacherService teacherService=new TeacherService(); 
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String method =request.getParameter("method");
		if(method==null) {
			add(request,response);
		}else if("stuadd".contentEquals(method)) {
			stuadd(request,response);
		}else if("teaadd".contentEquals(method)) {
			teaadd(request,response);
		}else if("zclogin".contentEquals(method)) {
			zclogin(request,response);
	}
	}
	private void zclogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String username = request.getParameter("username");
		String pwd = request.getParameter("pwd");	
		Teacher flag = teacherService.login1(username, pwd);
		if(flag!=null) {
			
			request.getRequestDispatcher("edit.jsp").forward(request, response);
		}else {
			response.sendRedirect("login1.jsp");
		}
		
	}
	private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("edit.jsp").forward(request, response);
		
	}

	private void stuadd(HttpServletRequest request, HttpServletResponse response) {
		
		
	}

	private void teaadd(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String idStr =request.getParameter("id");
		Integer id=null;
		if(idStr!=null&&!"".contentEquals(idStr)) {
			id=Integer.parseInt(idStr);
		}
		String name = request.getParameter("name");
		String password = request.getParameter("password");
		String username= request.getParameter("username");
		Teacher teacher=new Teacher(id,name,password,username);
		teacherService.sava(teacher);
		response.sendRedirect("login.jsp");
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
